package cn.hl.sfzy.controller;

import cn.hl.sfzy.entity.FM;
import cn.hl.sfzy.entity.Reply;
import cn.hl.sfzy.service.ReplyService;
import cn.hl.sfzy.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ReplyController
 * @Description TODO
 * @Author yangshen
 * @Date 2024年10月26日 14:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;
    @GetMapping
    public Result findByPage(@RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize,
                             Reply reply){
        PageHelper.startPage(pageNum,pageSize);
        List<Reply> replies = replyService.find(reply);
        PageInfo<Reply> replyPageInfo = new PageInfo<>(replies);
        return  Result.success(replyPageInfo);
    }
    @PostMapping
    public Result add(@RequestBody Reply reply){

        if (replyService.add(reply)!=0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") long id){
        if (replyService.delete(id)!=0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    @PutMapping
    public Result update(@RequestBody Reply reply){

        if (replyService.update(reply)!=0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @GetMapping("/selectByQuestionId/{id}")
    public Result selectById(@PathVariable Long id){
        List<Reply> reply=replyService.findByQuestionId(id);
        return Result.success(reply);
    }
}
